class Solution {
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) 
    {
        unordered_map<int,vector<int>> map;
        vector<int> v(numCourses);
        for(auto& e : prerequisites)
        {
            map[e[1]].push_back(e[0]);
            v[e[0]]++; 
        }

        queue<int> q;
        for(int i = 0; i < numCourses; i++)
        {
            if(v[i] == 0) q.push(i);
        }

        while(!q.empty())
        {
            int top = q.front(); q.pop();
            for(auto& it : map[top])
            {
                v[it]--;
                if(v[it] == 0) q.push(it);
            }
        }

        for(int i = 0; i < numCourses; i++)
        {
            if(v[i]) return false;
        }
        return true;
    }
};